
import { get } from '../axios'


interface GetQtClistParams {
    pageNum: number;
    agent?: any;
}
export const getQtClist = async ({pageNum,agent }: GetQtClistParams) => {
    return await get(`http://push2.eastmoney.com/api/qt/clist/get`, {
        params: {
            pn: pageNum,
            pz: 1000,
            po: 1,
            np: 1,
            fltt: 2,
            invt: 2,
            fid: 'f3',
            fs: 'm:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048',
            fields:
                'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152',
        },
        httpAgent: agent,
        httpsAgent: agent,
    })
};

export const getAllQtClist = async ({agent}:{agent?:any})=>{
    let totalPageNum: any=80;
    let pageNum: number = 1;
    let allQtList: any[] = [];
    while (pageNum < totalPageNum) {
        const qtData = await getQtClist({ pageNum: pageNum, agent});
        console.log(`获取第${pageNum}页数据，共${totalPageNum}页`);
        const { total, diff } = qtData.data.data;
        totalPageNum = Math.ceil(total / diff?.length);
        allQtList = allQtList.concat(diff);
        ++pageNum;  
    }

    return allQtList;
}

export const getQtKlines = async (secid: string,agent?:any) =>await get(`http://push2his.eastmoney.com/api/qt/stock/kline/get`, {
    params: {
        secid,
        fields1: 'f1,f2,f3,f4,f5,f6',
        fields2: 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61',
        klt: '101',
        fqt: '1',
        end: '20500101',
        lmt: '1000000',
    },
    httpAgent: agent,
    httpsAgent: agent,
});

export const getQtBoard = async (secucode: string) => get(`http://datacenter.eastmoney.com/securities/api/data/v1/get`, {
    params: {
        reportName: 'RPT_F10_CORETHEME_BOARDTYPE',
        columns:
            'SECUCODE,SECURITY_CODE,SECURITY_NAME_ABBR,NEW_BOARD_CODE,BOARD_NAME,SELECTED_BOARD_REASON,IS_PRECISE,BOARD_RANK,BOARD_YIELD,DERIVE_BOARD_CODE',
        quoteColumns: 'f3~05~NEW_BOARD_CODE~BOARD_YIELD',
        filter: `(SECUCODE="${secucode}")(IS_PRECISE="1")`,
        pageNumber: 1,
        sortTypes: 1,
        sortColumns: 'BOARD_RANK',
        source: 'HSF10',
        client: 'PC',
    },
});

// export const getAllStock: any = async () => {




//     let total = 100;
//     const line = genLine();
//     let length = 0;
//     let result: any[] = [];
//     let size: number = 0;




//     for (let i = 1; i <= total; i++) {
//         const { data } = await getQtClist({ line, pageNum: i });
//         length = data?.data?.total;
//         if (!size && data?.data?.diff?.length > 0) {
//             size = Math.ceil(data?.data?.total / data?.data?.diff?.length);
//             total = size;
//         }


//         result = result.concat(data?.data?.diff || []);

//     }

//     if (result?.length === length) {
//         return result?.sort(({ f12: next }: any, { f12: prev }: any) => {
//             return +next - +prev;
//         })?.map((item: any) => `${item.f13}.${item.f12}`);
//     } else {
//         return Promise.reject();
//     }

// }